package io.renren.modules.subway.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.Constant;
import io.renren.common.utils.PageUtils;
import io.renren.modules.subway.dao.StationsDao;
import io.renren.modules.subway.entity.LineEntity;
import io.renren.modules.subway.entity.StationEntity;
import io.renren.modules.subway.entity.StationLineEntity;
import io.renren.modules.subway.service.StationLineService;
import io.renren.modules.subway.service.StationsService;
import io.renren.modules.subway.vo.StationReqVo;
import io.renren.modules.subway.vo.StationRespVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@Service("stationsService")
public class StationsServiceImpl extends ServiceImpl<StationsDao, StationEntity> implements StationsService {

    @Autowired
    private StationLineService stationLineService;

    public PageUtils listStations(Map<String, Object> params) {
        //分页参数
        long curPage = Constant.DEF_PAGE;
        long limit = Constant.DEF_LIMIT;

        if(params.get(Constant.PAGE) != null){
            curPage = Long.parseLong((String)params.get(Constant.PAGE));
        }
        if(params.get(Constant.LIMIT) != null){
            limit = Long.parseLong((String)params.get(Constant.LIMIT));
        }

        //分页对象
        Page page = new Page(curPage, limit);

        //分页参数
        params.put(Constant.PAGE, page);

        IPage<StationEntity> ipage = baseMapper.listStations(page);

        List<StationRespVo> list = new ArrayList<>();
        for (StationEntity record : ipage.getRecords()) {
            StationRespVo station = new StationRespVo();
            station.setId(record.getId());
            station.setStatId(record.getStatId());
            station.setName(record.getName());
            station.setEarliestDepTime(record.getEarliestDepTime());
            station.setLatestDepTime(record.getLatestDepTime());
            if (record.getStatus() == 0) {
                station.setStatus("禁用");
            } else {
                station.setStatus("正常");
            }
            if (record.getIsTerminal() == 0) {
                station.setIsTerminal("否");
            } else {
                station.setIsTerminal("是");
            }
            List<String> lineNames = new ArrayList<>();
            for (LineEntity line : record.getLines()) {
                lineNames.add(line.getName());
            }
            station.setLineNames(StringUtils.join(lineNames, ","));
            list.add(station);
        }

        PageUtils result = new PageUtils(list, (int) page.getTotal(), (int) limit, (int) curPage);

        return result;
    }

    @Override
    public StationEntity getByID(int id) {
        return baseMapper.getByID(id);
    }

    @Override
    @Transactional
    public void createStation(StationReqVo req) {
        UUID uuid = UUID.randomUUID();
        StationEntity station  = new StationEntity();
        String statId = uuid.toString().replace("-", "");
        station.setStatId(statId);
        station.setStatus(req.getStatus());
        station.setIsTerminal(0);
        station.setName(req.getName());
        this.save(station);

        for (String lineId : req.getLineIds()) {
            StationLineEntity sl = new StationLineEntity();
            sl.setStatId(statId);
            sl.setLineId(lineId);
            stationLineService.save(sl);
        }
    }

}
